/*
 * @Author: MiNine
 * @Date: 2020-09-19 09:28:51
 * @LastEditors: MiNine
 * @LastEditTime: 2020-09-23 20:14:01
 * @Description: 
 */
const {
    ClaimOrder
} = require('../models/claimOrderModel');

const {
    Client
} = require('../models/clientModel');

const {
    Admin
} = require('../models/adminModel')

const dayjs = require('dayjs');

const {
    InsureOrder
} = require('../models/insureOrderModel');
/**
 * 添加 申请理赔订单
 * 
 */
async function addCompensation(req, res) {
    const {
        insureOrderId,
        voucher,
    } = req.body;
    const hasClaimOrder = await ClaimOrder.find({
        insureOrderId
    })
    if (hasClaimOrder.length > 0) {
        res.send({
            status: 0,
            msg: "重复提交"
        });
        return;
    }

    try {
        //    通过id获得保险订单
        let {
            clientId,
            adminId,
        } = await InsureOrder.findById(insureOrderId)
        // 添加新的理赔订单
        clientId = '5f68910f8e3d0000740048a3';

        const newClaimOrder = await ClaimOrder.create({
            insureOrderId,
            clientId,
            adminId,
            voucher,
            dueTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
        })
        if (newClaimOrder) {
            //添加理赔订单ID进入用户集合
            const log1 = await Client.findByIdAndUpdate(clientId, {
                $push: {
                    claimOrdList: newClaimOrder._id
                }
            });
            console.log(log1);
            //添加理赔订单ID进入销售员集合
            const log2 = await Admin.findByIdAndUpdate(adminId, {
                $push: {
                    claimOrdList: newClaimOrder._id
                }
            });
            console.log(log2);
            res.send({
                status: 1,
                msg: "addClaimOder successful"
            })
        } else {
            res.send({
                status: 0,
                msg: "addClaimOder fail"
            })
        }
    } catch (error) {
        console.log("addOrder ERROR:" + error);
        res.send({
            status: 0,
            msg: error
        })
    }
}

// 删除 根据理赔订单id删除文件
async function delCompensation(req, res) {
    const {
        _id
    } = req.body;
    try {
        const delClaimOrder = await ClaimOrder.findByIdAndDelete(_id);
        console.log(delClaimOrder);
        if (delClaimOrder) {
            // 删除对于用户的订单
            await Client.updateOne({
                _id: delClaimOrder.clientId
            }, {
                $pull: {
                    claimOderList: delClaimOrder._id
                }
            });

            // 删除对于管理员订单
            await Admin.updateOne({
                _id: delClaimOrder.adminId
            }, {
                $pull: {
                    claimOderList: delClaimOrder._id
                }
            })
            res.send({
                status: 1,
                data: delClaimOrder,
                msg: "delClaimOrder successful"
            })
        } else {
            res.send({
                status: 0,
                msg: "delClaimOrder fail , no target"
            })
        }
    } catch (error) {
        console.log("delClaimOrder ERROR:" + error);
        res.send({
            status: 0,
            msg: error
        })
    }
}

// 处理理赔申请
async function modClaim(req, res) {
    const {
        _id,
        status
    } = req.body;
    try {
        const updateClaimOrder = await ClaimOrder.findByIdAndUpdate(_id, {
            status
        }, {
            new: true //显示最新的数据
        });
        if (updateClaimOrder) {
            res.send({
                status: 1,
                msg: "updateClaimOrder successful"
            })
        } else {
            res.send({
                status: 0,
                msg: "updateClaimOrder fail, no target"
            })
        }
    } catch (error) {
        console.log("modClaim ERROR" + error);
        res.send({
            status: 0,
            msg: error
        })
    }
}


// 根据用户ID查看其理赔订单
async function getCompensations(req, res) {
    const {
        _id
    } = req.query
    try {
        const findClaimOrder = await Client
            .findById(_id)
            .populate("claimOrdList")
        const getClaimOrder = findClaimOrder.claimOrdList;
        if (getClaimOrder.length > 0) {
            const myOrder = await Client.findById(_id).populate({
                path: "belongAdminId"
            });
            const account = myOrder.account;
            const belongAdmin = myOrder.belongAdminId.account;
            console.log(getClaimOrder)
            res.send({
                data: {
                    getClaimOrder,
                    account,
                    belongAdmin
                },
                msg: "getCompensations successful",
                status: 1
            })
        } else {
            res.send({
                status: 0,
                msg: "getCompensations full"
            })
        }
    } catch (error) {
        console.log("getCompensations ERROR:" + error);
        res.send({
            status: 0,
            msg: error
        })
    }
}

async function modCompensation(req, res) {
    const {
        _id,
        status,
        adminId,
        clientId
    } = req.body
    await ClaimOrder.updateOne({
        _id
    }, {
        status,
        adminId,
        clientId
    })
    res.send({
        smg: "修改理赔订单信息成功",
        status: 1,
    })
}


// 管理员查看所有理赔申请
// 在理赔订单中找到所有状态值为1的订单返回去
async function getClaims(req, res) {
    try {
        const {
            _id
        } = req.body;
        const findPermission = await Admin.findOne({
            _id
        });
        if (findPermission) {
            if (findPermission.permission == 1 || findPermission.permission == 9) {
                const findClaim = await ClaimOrder.find()
                    .populate('adminId', 'nickName')
                    .populate('clientId', 'nickName')
                    .populate('insureOrderId', 'productName');
                var newClaim = findClaim.filter(item => item.status == 1);
            } else {
                const findClaim = await ClaimOrder.find({
                        adminId: _id
                    })
                    .populate('adminId', 'nickName')
                    .populate('clientId', 'nickName')
                    .populate('insureOrderId', 'productName');
                var newClaim = findClaim.filter(item => item.status == 1);
            }
        }
        if (newClaim) {
            res.send({
                data: newClaim,
                status: 1,
                msg: "getClaims successful"
            })
        } else {
            res.send({
                status: 0,
                msg: "getClaims full"
            })
        }
    } catch (error) {
        console.log("getClaims ERROR:" + error);
        res.send({
            status: 0,
            msg: error
        })
    }
};

async function updateClaim(req, res) {
    try {
        const {
            insureOrderId,
            update
        } = req.body;
        const findClaim = await ClaimOrder.findOne({
            insureOrderId
        })
        if (findClaim) {
            const updateClaim = await ClaimOrder.updateOne(findClaim, update);
            if (updateClaim) {
                res.send({
                    status: 1,
                    msg: "updateClaim successful"
                })
            } else {
                res.send({
                    status: 1,
                    msg: "updateClaim fail"
                })
            }
        } else {
            res.send({
                status: 1,
                msg: "updateClaim fail"
            })
        }
    } catch (error) {
        console.log("updateClaims ERROR:" + error);
        res.send({
            status: 0,
            msg: error
        })
    }
}


module.exports = {
    addCompensation,
    delCompensation,
    modClaim,
    getCompensations,
    modCompensation,
    getClaims,
    updateClaim
}